<?
  class Application_Model_FriendRefers extends Zend_Db_Table_Abstract
  {
    protected $_name = "friend_refers";
	protected $_rowClass = "FriendRefer";

	const referDailyLimit = 2;
  }

  class FriendRefer extends Zend_Db_Table_Row
  {
    public function save()
	{
	  $tab = new Application_Model_FriendRefers();

	  // check if user exists
	  $row = $tab->fetchRow($tab->select()->where('email = ?', $this->email));

	  if ($row)
	    throw new Zend_Db_Table_Exception('User was already contacted.');

	  $row = $tab->fetchRow($tab->select()->from('friend_refers', array('count(id) as count'))->where('stamp > ?', time()-24*60*60)->where('users_id = ?', $this->users_id));

	  if ($row->count >= $tab::referDailyLimit)
	    throw new Zend_Db_Table_Exception('Daily email limit reached');

	  parent::save();
	}
  }
